﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace IE
{
    [XmlType("Config")]
    public class Config
    {
        private string userName;
        [XmlAttribute("userName")]
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        private string password;
        [XmlAttribute("password")]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        private string saveFilePath;
        [XmlAttribute("saveFilePath")]
        public string SaveFilePath
        {
            get { return saveFilePath; }
            set { saveFilePath = value; }
        }

        private string loadInfoPath;
        [XmlAttribute("loadInfoPath")]
        public string LoadInfoPath
        {
            get { return loadInfoPath; }
            set { loadInfoPath = value; }
        }

        public Config()
        {
            string defaultDir = System.Windows.Forms.Application.StartupPath;
            saveFilePath = defaultDir + Path.DirectorySeparatorChar + DateTime.Now + ".xls";
            LoadInfoPath = defaultDir + Path.DirectorySeparatorChar + "info.xls";
            UserName = "zhanghuibing@9jlicai.com";
            Password = "A711618A";
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>配置文件内存</returns>
        static public Config loadConfig(string filePath)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(Config));
            Config result = null;
            //using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            using (StreamReader stream = new StreamReader(filePath, Encoding.UTF8))
            {
                try
                {
                    result = (Config)deserializer.Deserialize(stream);
                    stream.Close();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
            return result;
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="config">配置文件内存</param>
        static public void saveConfig(string filePath, Config config)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.Indent = true;
            settings.ConformanceLevel = ConformanceLevel.Auto;
            settings.IndentChars = "\t";
            settings.OmitXmlDeclaration = false;
            XmlSerializer serializer = new XmlSerializer(typeof(Config));
            //using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            using (XmlWriter writer = XmlWriter.Create(filePath, settings))
            {
                try
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add(string.Empty, string.Empty);
                    XmlSerializer formatter = new XmlSerializer(typeof(Config));
                    formatter.Serialize(writer, config, ns);
                    writer.Close();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
        }
    }
}
